package com.it.park.rms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.rms.client.model.Menu;
import com.it.park.rms.client.service.MenuService;
import com.it.park.rms.common.Page;
import com.it.park.rms.common.PageData;
import com.it.park.rms.common.Result;
import com.it.park.rms.common.util.empty.EmptyUtil;
import com.it.park.rms.constant.Constant;

@Controller
@RequestMapping("/admin/menu")
public class AdminMenuController {
	private String rootPath=Constant.ADMIN_PATH;

	@Autowired
	private MenuService menuService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<Menu> listRows(HttpServletRequest request){
		PageData<Menu> pageData = new PageData<>();
		int pageNow = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		Menu menu = new Menu();
		menu.setCategory(0);
		Integer total = menuService.selectTotal(menu);
		Page page = new Page(pageNow,total,rows);
		menu.setStartPos(page.getStartPos());
		menu.setPageSize(page.getPageSize());
		List<Menu> list = menuService.selectPagination(menu);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("listMenus")
	@ResponseBody
	public List<Menu> listMenus(HttpServletRequest request){
		String id = request.getParameter("id");
		List<Menu> list  = null;
		Menu menu = new Menu();
		menu.setCategory(0);
		if(id != null && !id.equals("")){
			menu.setPid(Integer.parseInt(id));
			list = menuService.listMenus(menu);
		}else{
			menu.setPid(0);
			list = menuService.listMenus(menu);
		}
		for (Menu record : list) {
			Menu m = new Menu();
			m.setPid(record.getId());
			m.setCategory(0);
			List<Menu> ls = menuService.listMenus(m);
			if(EmptyUtil.isEmpty(ls)){
				record.setState("open");
			}
		}
		return list;
	}
	
	/**
	 * 菜单页面
	 * @return
	 */
	@RequestMapping("menuList")
	public String menuList(HttpServletRequest request){
			return rootPath+"menu/menuList";
	}
	
	/**
	 *单个通知详情 
	 */
	@RequestMapping("detail")
	public String detail(HttpServletRequest request){
		return "jsp/menu/menuDetail";
	}
	

	@RequestMapping("save")
	@ResponseBody
	public Result<String> save(Menu menu,HttpServletRequest request){
		Result<String> json = new Result<>();
		Integer maxSort = menuService.selectMaxSort(menu.getPid());
		menu.setSort(maxSort != null?maxSort+1:1);
		boolean success = menuService.insertSelective(menu);
		if(success){
			json.setMessage("添加成功!");
			menuService.updateState(menu.getPid());
		}else{
			json.setMessage("添加失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(@RequestParam("id") int id,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = false;
		Menu menu = menuService.selectByPrimaryKey(id);
		menuService.deleteByPrimaryKey(id);
		Integer maxSort = menuService.selectMaxSort(menu.getPid());
		if(maxSort == null){
			Menu menu_ = new Menu();
			menu_.setId(menu.getPid());
			menu_.setState("open");
			success = menuService.updateByPrimaryKeySelective(menu_);
		}
		if(success){
			json.setMessage("删除成功!");
		}else{
			json.setMessage("删除失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(Menu menu,HttpServletRequest request){
		Result<String> json = new Result<>();
		boolean success = menuService.updateByPrimaryKeySelective(menu);
		if(success){
			json.setMessage("修改成功!");
		}else{
			json.setMessage("修改失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("selectAll")
	@ResponseBody
	public List<Menu> selectAll(){
		return menuService.selectAll();
	}
	
	@RequestMapping("listMenusTree")
	@ResponseBody
	public List<Menu> listMenusTree(HttpServletRequest request){
		String id = request.getParameter("id");
		List<Menu> list  = null;
		Menu menu = new Menu();
		if(id != null && !id.equals("")){
			menu.setPid(Integer.parseInt(id));
			list = menuService.listMenus(menu);
		}else{
			menu.setPid(0);
			list = menuService.listMenus(menu);
		}
		return list;
	}
}
